home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / discex.zip / DISCEX.C next >
Text File  |  1988-10-24  |  4KB  |  157 lines

  1. /*************************************************************************
  2.  
  3. DiscEx
  4.  
  5. PC disk excerciser
  6.  
  7. Use this program to burn in a disk drive or test for intermittent errors
  8. or to force a "dying" disk to fail.
  9.  
  10. Usage:  DiscEx <no passes>
  11.  
  12.         <no passes> in the number of times you want DiscEx to execute
  13.         the random seek test.  Each pass executes 500 random positions
  14.         and reads.
  15.         
  16.         The test is performed on the current (default) drive.
  17.         
  18. License and copyright:
  19.  
  20.         The program (DiscEx.exe and DiscEx.c) is copyright (c) 1988 by 
  21.         Steven D. Stern.  
  22.         
  23.         If you find this program useful, please send a couple of bucks
  24.         to the author:
  25.            Steven D. Stern
  26.            Crown Software Corporation
  27.            900 North Michigan Avenue, Suite 900
  28.            Chicago, IL  60611
  29.            
  30.         The author may contacted via Compuserve:  70327,135
  31.         
  32. *************************************************************************/
  33.         
  34.           
  35. #include <dos.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <io.h>
  39. #include <memory.h>
  40. #include <sys\timeb.h>
  41.  
  42. #define BUFSIZE 4096
  43.  
  44. long random();
  45.  
  46. main(int argc, char *argv[] )
  47.    {
  48.    long bytes,                  /* max file size in bytes */
  49.         blocks,                 /* max no. of BUFSIZE blocks */
  50.         position;
  51.    long dpi;
  52.    int ret;
  53.    int pass;                    /* no of times to repeat random reads */
  54.    struct timeb TIME;   
  55.    struct diskfree_t drive;
  56.    char filebuf[BUFSIZE];
  57.    char filename[9];
  58.    FILE *testfile;
  59.    
  60.    if (argc > 1) pass = atoi(argv[1]);
  61.    
  62.    printf("DiscEx:  The PC disk excerciser\n");
  63.    printf("Copyright (c) 1988, Steven D. Stern\n\n");
  64.    
  65.    _dos_getdiskfree(0,&drive);   /* how much free space on disk? */
  66.    
  67.    bytes =                       /* convert it to byte-size      */
  68.       (long)drive.bytes_per_sector * 
  69.       (long)drive.sectors_per_cluster * 
  70.       (long)drive.avail_clusters;
  71.    
  72.    strcpy(filebuf,"DXXXXXXX");
  73.    
  74.    mktemp(filebuf);
  75.    
  76.    testfile = fopen(filebuf,"a+b");
  77.    
  78.    if (testfile == NULL)
  79.       {
  80.       printf("Unable to open scratch file: %s\n",filebuf);
  81.       exit(1);
  82.       }
  83.       
  84.    printf("Creating scratch file: %s\n",filebuf);
  85.    strcpy(filename,filebuf);
  86.    
  87.    setvbuf(testfile,NULL,_IOFBF,4096);
  88.    
  89.    blocks = bytes / BUFSIZE;
  90.    
  91.    memset(filebuf,0xF5,BUFSIZE);        /* load buf with some data */
  92.    
  93.    printf("Loading scratch file for %ld bytes\n",blocks*BUFSIZE);
  94.    
  95.    for (dpi = 0; dpi < blocks; dpi++)
  96.       {
  97.       ret = fwrite(filebuf,BUFSIZE,1,testfile);
  98.       if (ret != 1)
  99.          {
  100.          printf("Error writing scratch file\n");
  101.          fclose(testfile);
  102.          unlink(filename);
  103.          exit(1);
  104.          }
  105.       }
  106.    fflush(testfile);
  107.    printf("Load successful\n");
  108.    
  109.    printf("Beginning sequential read test\n");
  110.    
  111.    fseek(testfile,0L,SEEK_SET);         /* rewind the file */
  112.    for (dpi = 0; dpi < blocks; dpi++)
  113.       {
  114.       ret = fread(filebuf,BUFSIZE,1,testfile);
  115.       }
  116.    printf("Sequential read test complete\n");
  117.    
  118.    
  119.    while (pass)
  120.       {
  121.       ftime(&TIME);
  122.       srand(TIME.millitm);
  123.       printf("%d pass(es) remaining",pass);
  124.       fflush(stdout);
  125.       for (dpi = 0; dpi < 500L; dpi++)
  126.          {
  127.          position = random(bytes);
  128.          fseek(testfile,position,SEEK_SET);
  129.          fread(filebuf,1,1,testfile);
  130.          }
  131.       printf("... complete\n");
  132.       pass--;
  133.       }
  134.    printf("Random seek test complete\n");
  135.    printf("Erasing scratch file\n");
  136.    
  137.    fclose(testfile);
  138.    unlink(filename);
  139.    }
  140.  
  141. long random( long top )
  142.    {
  143.    double scale;
  144.    int i;
  145.    double d;
  146.    
  147.    i = rand();
  148.    scale = (double)top / (double)32768;
  149.    d= scale * (double) i;
  150.    if ( (long)d > top)
  151.       {
  152.       printf("Random problem\n");
  153.       return (0L);
  154.       }
  155.    return ( (long)d );
  156.    }
  157.